package com.tianxiabuyi.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理工具类
 * Created by yang on 2015/2/12.
 */
public final class DateUtil {
    public static final String FormatDayToChinaTime24H = "yyyy年MM月dd日";
    public static String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_FORMAT = "yyyy-MM-dd";
    public static String TIME_FORMAT = "HH:mm:ss";

    private DateUtil() {
    }

    /**
     * 返回年份
     * @param date 日期
     * @return 返回年份
     */
    public static int getYear(Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.YEAR);
    }

    /**
     * 返回月份
     * @param date 日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.MONTH) + 1;
    }

    /**
     * 返回日份
     * @param date  日期
     * @return 返回日份
     */
    public static int getDay(Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.DAY_OF_MONTH);
    }

    /**
     * Parse date like "yyyy-MM-dd".
     */
    public static Date parseDate(String d) {
        try {
            return new SimpleDateFormat(DATE_FORMAT).parse(d);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Parse date like "yyyy-MM-dd HH:mm:ss".
     */
    public static Date parseDateTime(String d) {
        try {
            return new SimpleDateFormat(DATETIME_FORMAT).parse(d);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 以指定的格式来格式化日期
     */
    public static String formatDateByFormat(Date date, String format) {
        String result = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 常用的格式化日期
     */
    public static String formatDate(Date date) {
        return formatDateByFormat(date, "yyyy-MM-dd");
    }

    /**
     * 根据当前日期计算n天后的日期
     */
    public static Date afterNDay(Date date, int n) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, n);
        Date destDay = c.getTime();
        return destDay;
    }

    /**
     * 两个日期间的天数
     */
    public static int getIntervalDays(Date startday, Date endday) {
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        long sl = startday.getTime();
        long el = endday.getTime();
        long ei = el - sl;
        return (int) (ei / (1000 * 60 * 60 * 24));
    }
    public static void main(String[] args) {
  	  Calendar calendar = Calendar.getInstance();
  	  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  	  calendar.add(Calendar.DATE, 15);
  	   System.out.println(sdf.format(calendar.getTime()));
	}
}